package mil.af.amc.adpetracker.web.actions.struts1.User;

import mil.af.amc.adpetracker.service.AccountService;
import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import org.apache.struts.action.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action to reset a user's password
 *
 * @author TSgt Jason Ferguson
 */

public class ResetPasswordAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(ResetPasswordAction.class);

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {


        DynaActionForm form = (DynaActionForm) actionForm;
        String email = (String) form.get("email");
        
        try {
            accountService.resetPassword(email);
            //userService.resetPassword(email);
        } catch (ServiceException e) {
            ActionMessage message = new ActionMessage(e.getKey(), "Unable to reset user password");
            saveError(httpServletRequest, message);

            return actionMapping.findForward("success");
        }

        ActionMessage message = new ActionMessage("Password reset. You should receive an email shortly with new details");
        saveError(httpServletRequest, message);

        log.info("Password reset for username: " + email);

        return actionMapping.findForward("success");

    }
}
